#!/bin/bash
# Author: CryoByte33

# This is nested for good reason, zenity won't exit the entire script if the 'x' button is pressed.
# Nesting it forces execution only if an option is selected.
if zenity --question --title="免责声明" --text="此程序将卸载CryoUtilities.\n\n<b>免责声明:</b> 是否要继续?" --width=600 2>/dev/null; then
  if zenity --question --title="还原" --text="你想恢复CryoUtilities所做的调整吗?\n\n<b>备注:</b> 这不会将游戏数据移动到固态硬盘上的原始位置." --width=600 2>/dev/null; then
    # Ask for password
    hasPass=$(passwd -S "$USER" | awk -F " " '{print $2}')
    if [[ $hasPass != "P" ]]; then
      zenity --error --title="密码错误" --text="Password is not set, please set one in the terminal with the <b>passwd</b> command, then run this again." --width=400 2>/dev/null
      exit 1
    fi
    PASSWD="$(zenity --password --title="输入密码" --text="输入桌面模式用户密码(不是Steam帐户！)" 2>/dev/null)"
    echo "$PASSWD" | sudo -v -S
    ans=$?
    if [[ $ans == 1 ]]; then
      zenity --error --title="密码错误" --text="提供的密码不正确，请再次运行此命令并提供正确的密码." --width=400 2>/dev/null
      exit 1
    fi
    # Revert everything to stock
    sudo bash "$HOME"/.cryo_utilities/cryo_utilities stock
  fi
  # Delete install directory
  rm -rf "$HOME/.cryo_utilities"

  # Remove Desktop icons
  rm -rf "$HOME"/Desktop/CryoUtilitiesUninstall.desktop 2>/dev/null
  rm -rf "$HOME"/Desktop/CryoUtilities.desktop 2>/dev/null
  rm -rf "$HOME"/Desktop/UpdateCryoUtilities.desktop 2>/dev/null
  rm -rf "$HOME"/Downloads/CryoUtilitiesUninstall.desktop 2>/dev/null
  rm -rf "$HOME"/Downloads/UpdateCryoUtilities.desktop 2>/dev/null
  # Remove Start Menu shortcuts
  rm -rf "$HOME"/.local/share/applications/CryoUtilitiesUninstall.desktop 2>/dev/null
  rm -rf "$HOME"/.local/share/applications/CryoUtilities.desktop 2>/dev/null
  rm -rf "$HOME"/.local/share/applications/UpdateCryoUtilities.desktop 2>/dev/null
  update-desktop-database ~/.local/share/applications

  # Remove icon from KDE
  xdg-icon-resource uninstall cryo-utilities 2>/dev/null
fi
